import '../../router/navigator_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../share/my_color.dart';

/*
* title 和 titleWidget 优先使用 title
*
* */

PreferredSizeWidget MyAppBar(
  BuildContext context, {
  Key? key,
  String? title,
  Color titleColor = MyColors.color_34_34_34,
  double titleFont = 18,
  FontWeight titleFontWeight = FontWeight.w500,
  Widget? titleWidget,
  Widget? leading,
  Color backgroundColor = MyColors.color_255_255_255,
  double elevation = 0, //底部阴影
  IconThemeData iconTheme = const IconThemeData(color: MyColors.color_34_34_34, size: 24),
  List<Widget>? actions,
  PreferredSizeWidget? bottom,
  bool centerTitle = true,
  Brightness brightness = Brightness.light,
  double titleSpacing = NavigationToolbar.kMiddleSpacing,
  bool automaticallyImplyLeading = true, //隐藏左边返回按钮
  Function? backFunction, //返回按钮被点击
}) {
  Widget? leadingWidget = leading;
  if (automaticallyImplyLeading == true) {
    leadingWidget = leading ??= IconButton(
      iconSize: 20,
      icon: Icon(Icons.arrow_back_ios),

      onPressed: () {
        if (backFunction == null) {
          NavigatorUtil.pop(context);
        } else {
          backFunction();
        }
      },
    );
  }

  if (titleWidget == null && title != null) {
    titleWidget = Text(
      title,
      style: TextStyle(color: titleColor, fontSize: titleFont, fontWeight: titleFontWeight),
    );
  }

  return AppBar(
    title: titleWidget,
    backgroundColor: backgroundColor,
    actions: actions,
    iconTheme: iconTheme,
    elevation: elevation,
    bottom: bottom,
    leading: leadingWidget,
    automaticallyImplyLeading: automaticallyImplyLeading,
    centerTitle: centerTitle,
    brightness: brightness,
    titleSpacing: titleSpacing,
  );
}
